﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TracNghiem.Areas.Admin.Models;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace TracNghiem.Controllers
{
    public class baihocnewController : Controller
    {
        //
        // GET: /baihocnew/

        Bai_VietModels baihocmodels = new Bai_VietModels();
        DichvuModel dichvu = new DichvuModel();

        public static string group = "";
        public ActionResult Index(string id)
        {
            ViewBag.menu = xulymenu_cobaihoc(id);
            ViewBag.khuyenmai = XuLy("http://google.com.vn/");
            group = id;
            //List<DichvuModel> dv = dichvu.getDichvu();
            //ViewBag.menu = dv;
            //return View(bh);
            return View();
        }
        public ActionResult danhmuc(int id)
        {

            ViewBag.menu = xulymenu_cobaihoc(id.ToString());
            List<Bai_VietModels> bh = baihocmodels.getbaihoc_DanhMuc(id);

            return View(bh);
        }
        public ActionResult chitiet(int id)
        {
            //  vewba
            try
            {
                ViewBag.menu = xulymenu_cobaihoc(group);
                Bai_VietModels _baihocmodels = baihocmodels.getbaihoc().Find(delegate(Bai_VietModels s) { return s.id == id.ToString(); });
                List<Noi_DungModels> nd = new Noi_DungModels().GETNOIDUNG(id);
                ViewBag.name = _baihocmodels.name;
                ViewBag.dichvu = _baihocmodels.tendichvu;
                ViewBag.dichvuid = _baihocmodels.dichvu_id;
                return View(nd);
            }
            catch
            {
                return RedirectToAction("Index", new { id = "0" });
            }
        }
        //
        public static string xulymenu()
        {
            List<string> kq = new List<string>();
            DichvuModel dichvu = new DichvuModel();
            Bai_VietModels baiviet = new Bai_VietModels();
            string htmlmenu = "";
            //lấy danh mục cha
            List<DichvuModel> dv = dichvu.getDichvu_byid2(0,0);



            foreach (DichvuModel a in dv)
            {

                //lấy danh mục con của danh mục cha


                List<DichvuModel> dvc = dichvu.getDichvu_byid2(int.Parse(a.id),0);
                if (dvc.Count > 0)
                {
                    htmlmenu += "<li class=\"parent first\"><a class=\"collapsed\"  >" + a.name + "</a>";
                    htmlmenu += "<ul style='display: none;' id='uldvuinternet'>";
                    foreach (var b in dvc)
                    {
                        htmlmenu += "<li><a href='/tracnghiem/baihocnew/danhmuc/" + b.id + "' >" + b.name + "</a></li>";
                        //them bai viet


                    }
                    htmlmenu += "</ul></li>";


                }

                else { htmlmenu += "<li class=\"parent first\"><a href='/tracnghiem/baihocnew/danhmuc/" + a.id + "' >" + a.name + "</a>"; }




            }
            return htmlmenu;

        }
        public  string xulymenu_cobaihoc(string id)
        {
            int stt = 1;
            List<string> kq = new List<string>();
            DichvuModel dichvu = new DichvuModel();
            Bai_VietModels baiviet = new Bai_VietModels();
            string htmlmenu = "";
            //lấy danh mục cha
            List<DichvuModel> dv = dichvu.getDichvu_byid2(0,Convert.ToInt32(id));

            UrlHelper u = new UrlHelper(this.ControllerContext.RequestContext);

            foreach (DichvuModel a in dv)
            {
                
                //lấy danh mục con của danh mục cha
                List<DichvuModel> dvc = dichvu.getDichvu_byid2(int.Parse(a.id), Convert.ToInt32(id));
                if (dvc.Count > 0)//neu co menu con
                {
                    htmlmenu += "<li class=\"parent\"><a class=\"collapsed\"  >" +a.name + "</a>";
                    htmlmenu += "<ul style='display: none;' id='uldvuinternet'>";
                    foreach (var b in dvc)
                    {
                        htmlmenu += "<li class=\"parent\"><a><i><b>" + b.name + "</b></i></a>";
                        //them bai viet
                        htmlmenu += "<ul style=\"display: none;\">";
                        //can lap
                        List<Bai_VietModels> bv = baiviet.getbaihoc_DanhMuc(int.Parse(b.id));
                        foreach (var item in bv)
                        {
                           // htmlmenu += "<li><a href='/tracnghiem/baihocnew/chitiet/" + item.id + "' >" + item.name + "</a></li>";
                            htmlmenu += "<li><a href='" + u.Action("chitiet", "baihocnew", new { id = item.id }) + "'>" + item.name + "</a></li>";
                        }

                        htmlmenu += "</ul></li>";

                    }
                    htmlmenu += "</ul></li>";
                }

                else //khong co menu con
                {
                    if (stt==1)
                    {
                         htmlmenu += "<li class=\"parent first\"><a class=\"collapsed\"  >" + a.name + "</a>";
                    }
                    else
                    { htmlmenu += "<li class=\"parent\"><a class=\"collapsed\"  >" + a.name + "</a>"; }

                    htmlmenu += "<ul style=\"display: none;\">";
                    //can lap
                    List<Bai_VietModels> bv = baiviet.getbaihoc_DanhMuc(int.Parse(a.id));
                    foreach (var item in bv)
                    {
                        //htmlmenu += "<li><a href='/tracnghiem/baihocnew/chitiet/"+item.id+"' >" + item.name + "</a></li>";
                        //htmlmenu += "<li>" + u.Action("chitiet", "baihocnew", new { id=item.id}) + "</li>";
                        htmlmenu += "<li><a href='" + u.Action("chitiet", "baihocnew", new { id = item.id }) + "' >" + item.name + "</a></li>";
                    }

                    htmlmenu += "</ul>";
                    stt++;
                }
            }
            return htmlmenu;
        }

        //xu ly lay tin khuyen mãi
        public static string layMaNguon(string strLink)
        {
            string strContent = "";
            try
            {
                WebRequest objWebRequest = WebRequest.Create(strLink);

                objWebRequest.Credentials = CredentialCache.DefaultCredentials;

                WebResponse objWebResponse = objWebRequest.GetResponse();
                Stream receiveStream = objWebResponse.GetResponseStream();
                StreamReader readStream = new StreamReader(receiveStream, System.Text.Encoding.UTF8);

                strContent = readStream.ReadToEnd();
                objWebResponse.Close();
                readStream.Close();
            }
            catch (Exception)
            {
                return null;
            }
            return strContent;
        }
       
        public static string layTin(string input, string ma)
        {
            string kq = "";
            string pattern = ma;

            if (input != null)
            {
                MatchCollection matches = Regex.Matches(input, pattern);


                foreach (Match m in matches)
                {
                    kq += m.Groups[1];
                    return kq;

                }
            }

            return kq;




        }
        public static string XuLy(string HOST)
        {
            string kq = "";
            //layMaNguon(HOST);

            kq += layTin(layMaNguon(HOST), "<body>(.*?)</body>");
 
            //tiếp tục lấy code trong trang 1

           
            return kq;

        }
    }
}
